<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.File" %>
<%
	Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(getServletContext().getRealPath("/") + "web.xml"));
	doc.getDocumentElement().normalize();
	String dbhost = doc.getElementsByTagName("host").item(0).getTextContent();
	String dbport = doc.getElementsByTagName("port").item(0).getTextContent();
	String dbuser = doc.getElementsByTagName("user").item(0).getTextContent();
	String dbpass = doc.getElementsByTagName("pass").item(0).getTextContent();
	String dbname = doc.getElementsByTagName("database").item(0).getTextContent();
	String dbconnString = "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?setUnicode=true&characterEncoding=UTF-8";
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection dbconn = DriverManager.getConnection(dbconnString, dbuser, dbpass);
	Statement query = dbconn.createStatement();
%>

<%
	String type = null;
	int maTS = 0, id = 0;
	String hoten = null, username = null;
	HttpSession ss = request.getSession();
	if (ss.getAttribute("isLogin") == null) {
		response.sendRedirect("login.jsp");
	} else if (ss.getAttribute("isLogin").equals(false)) {
		response.sendRedirect("login.jsp");
	} else {
		type = ss.getAttribute("type").toString();
		if (type.equals("TS")) {
			maTS = Integer.parseInt(ss.getAttribute("maTS").toString());
			hoten = ss.getAttribute("hoten").toString();
		} else {
			id = Integer.parseInt(ss.getAttribute("id").toString());
			username = ss.getAttribute("username").toString();
		}
	}
%>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Thi trắc nghiệm online</title>
		<link rel="stylesheet" href="css/style.css" type="text/css" />
		<script type="text/javascript" src="js/general_functions.js"></script>
		<link rel="stylesheet" href="css/baithi.css" type="text/css" />
	</head>
	<body>

		<!-- BEGIN Header -->
		<div id="header">
			<div id="header-content">
				<h1><span>styleshout</span></h1>
				<h2 id="slogan">Thi trắc nghiệm online</h2>
				<ul id="header-links">
					<li>
						<%
							if (type.equals("TS")) {
								out.print(hoten);
							} else {
								out.print(username);
							}
						%>
					</li>
					<li><a href="logout.jsp">Đăng xuất</a></li>
				</ul>
				<!-- Menu Tabs -->
				<ul>
					<li><a href="index.jsp" title="Trang chủ"><span>Trang chủ</span></a></li>
					<li><a href="thi.jsp" title="Thi trắc nghiệm"><span>Thi trắc nghiệm</span></a></li>
					<li><a href="thongke.jsp" title="Tổng hợp kết quả thi"><span>Tổng hợp kết quả thi</span></a></li>
					<li><a href="cauhoi.jsp" title="Soạn câu hỏi"><span>Soạn câu hỏi</span></a></li>
				</ul>
			</div>
		</div>
		<!-- END Header -->


		<!-- BEGIN Content -->
		<div id="content">

			<h1 style="text-align:center;">KẾT QUẢ BÀI THI</h1>
			<div id ="baithi_info">
				<%
					String maBT = request.getParameter("mabt");
					ResultSet rs = query.executeQuery("SELECT * FROM  ( (baithi left join thisinh on thisinh = maTS) "
							+ " left join dethi on dethi = maDT ) left join loaidethi on loaidethi = maLDT  WHERE maBT = " + maBT);
					out.println("<table>");
					if (rs.next()) {
						out.println("<tr><th>Thí sinh </th><td>" + rs.getString("hoten") + "</td></tr>");
						out.println("<tr><th>Mã bài thi </th><td>" + maBT + "</td></tr>");
						out.println("<tr><th>Mã đề thi </th><td>" + Integer.toString(rs.getInt("dethi")) + "</td></tr>");
						out.println("<tr><th>Loại đề thi </th><td>" + rs.getString("tenloai") + "</td></tr>");
						out.println("<tr><th>Thời gian làm bài </th><td>" + Integer.toString(rs.getInt("thoigian")) + " phút" + "</td></tr>");
						out.println("<tr><th>Thời gian bắt đầu </th><td>" + rs.getTimestamp("thoigianbd").toString() + "</td></tr>");
						out.println("<tr><th>Thời gian nộp bài </th><td>" + rs.getTimestamp("thoigiankt").toString() + "</td></tr>");
						if (rs.getFloat("diem") == -1) // bài thi phạm qui
						{
							out.println("<tr><th>Điểm </th><td><b><i style='color:red'> phạm qui </i></b></td></tr>");
						} else {
							out.println("<tr><th>Điểm </th><td><b>" + Float.toString(rs.getFloat("diem")) + "</b></td></tr>");
						}
					}
					rs.close();
					out.println("</table>");
				%>
			</div>
			<div id="baithi_noidung">
				<%
					rs = query.executeQuery("select * from ct_baithi CTBT join baithi BT on CTBT.baithi = BT.maBT "
							+ " join ct_dethi CTDT on (BT.dethi = CTDT.dethi and CTBT.cauhoi = CTDT.cauhoi) "
							+ " join cauhoi CH on CTDT.cauhoi = CH.maCH where CTBT.baithi = " + maBT + " order by CTDT.thutu asc");
					int i = 1;
					while (rs.next()) {
						out.println("<div id='cauhoi'>");
						out.println("<div id='noidung'><b>Câu hỏi " + Integer.toString(i) + " : </b>" + rs.getString("noidung") + "</div>");
						out.println("<div id='luachon'><i>Lựa chọn 1 : </i>" + rs.getString("luachon1") + "</div>");
						out.println("<div id='luachon'><i>Lựa chọn 2 : </i>" + rs.getString("luachon2") + "</div>");
						out.println("<div id='luachon'><i>Lựa chọn 3 : </i>" + rs.getString("luachon3") + "</div>");
						out.println("<div id='luachon'><i>Lựa chọn 4 : </i>" + rs.getString("luachon4") + "</div>");
						out.println("<div id='traloi'><b>Lựa chọn trả lời : " + Integer.toString(rs.getInt("luachon")) + "</b></div>");
						if (rs.getInt("luachon") == rs.getInt("dapan"))
							out.println("<div id='dapan'><i>Đúng</i></div>");
						else
							out.println("<div id='dapan'><i>Sai. </i> <b> Đáp án đúng : " + Integer.toString(rs.getInt("dapan")) + "</b></div>");
						out.println("</div>");
						i++;
					}
					rs.close();
				%>
			</div>
		</div>
		<!-- END Content -->

		<!-- BEGIN footer -->
		<div id="footer">
			<div id="footer-content">
				<div class="footer-left">
					<p style="text-align: right">
						&copy; copyright 2006-2011 <strong class="separator">www.styleshout.com</strong>
					</p>
				</div>
			</div>
		</div>
		<!-- END footer -->

	</body>
</html>

<% dbconn.close();%>